package quq.missq.utils;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import quq.missq.MissQApplication;



public class FileUtil {
	
	private static final String appPath = "/XL_IM";

	/**
	 * 获取该APP根目录
	 * @throws Exception  
	 * @author XieWenjun
	 */
	public static File getAppPath() {
		String path = getSDCardPath() + "/" + appPath;
		File folder = new File(path);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		return folder;
	}

	/**
	 * 获取SD卡路径
	 * @return
	 * @throws Exception  
	 * @author XieWenjun
	 */
	public static String getSDCardPath() {
		File storageDirectory = null;
		if( isSDCardExist() ){
			storageDirectory = Environment.getExternalStorageDirectory();
		}
		return storageDirectory.toString();
	}

	/**
	 * SD卡是否存在
	 * @return  
	 * @author XieWenjun
	 */
	public static boolean isSDCardExist() {
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}
	
	/**
	 * 删除图片-根据路径
	 * @param path 
	 */
	public static void deleteTempFile(String path) {
		File file = new File(path);
		deleteTempFile(file);
	}
	
	/**
	 * 删除图片-根据文件
	 */
	public static void deleteTempFile(File file) {
		if (file.exists()) {
			file.delete();
		}
	}
	
	/**
	 * 创建文件
	 */
	public static File newFile(String path, String name) {
		return new File(newFile(path), name);
	}
	
	/**
	 * 创建文件夹
	 */
	public static File newFile(String path) {
		File dir = null;
		try {
			dir = new File(FileUtil.getAppPath(), path);
		} catch (Exception e) {
			Log.e("创建相册文件夹失败",e.getMessage());
		}
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return dir;
	}

	/**
	 * 将RES的GIF保存到SD卡
	 * @param resId
	 * @param file
	 * @return  
	 * @author XieWenjun
	 */
	public static String saveGif(int resId, File file) {
		InputStream is = null;
		ByteArrayOutputStream baos = null;
		FileOutputStream fos = null;
		try {
			is = MissQApplication.getInstance().getResources().openRawResource(resId);
	        byte[] b = new byte[1024];
	        baos = new ByteArrayOutputStream();
	        int len = -1;
	        while ((len = is.read(b)) != -1) {
	        	baos.write(b, 0, len);
	        }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				baos.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		byte[] result = baos.toByteArray();
		try {
			fos = new FileOutputStream(file);
	        fos.write(result);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return file.getAbsolutePath();
	}


    /** 保存方法 */
    public static void saveBitmap(Bitmap bitmap) {
        File dy= new File(Environment.getExternalStorageDirectory()+"/jiamei");
        try {
            if (!dy.exists()) {
                dy.mkdirs();
            }
            File f = new File(Environment.getExternalStorageDirectory()+"/jiamei/","hasuserhead.jpg");
            if(!f.exists())
            {
                f.createNewFile();
            }

            FileOutputStream out = new FileOutputStream(f);

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



}
